/**
 * @filename:TestwaveServiceImpl Jan 9, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.Testwave;
import com.wonder.app.domain.Testwavest;
import com.wonder.app.domain.Vswave;
import com.wonder.app.domain.Vswavest;
import com.wonder.app.mapper.TestwaveDao;
import com.wonder.app.mapper.TestwavestDao;
import com.wonder.app.service.TestwaveService;
import com.wonder.app.request.TestwaveRequest;
import com.wonder.app.service.TestwavestService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**   
 * @Description:TODO(测试波形设置服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class TestwaveServiceImpl  extends ServiceImpl<TestwaveDao, Testwave> implements TestwaveService  {

    /**
     * 验证测试波形设置信息
     * 
     * @param testwave
     * @param type
     * @return String
     */
	@Autowired
	private TestwavestService testwavestService;
	@Autowired
	private TestwavestDao testwavestDao;
	@Autowired
	private  TestwaveService testwaveService;
	@Autowired
	private  TestwaveDao testwaveDao;
    @Override
    public String validate(Testwave testwave,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:测试波形设置分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Testwave>   //分页记录
	 */
	@Override 
	public IPage<Testwave> selectList(TestwaveRequest request, Page<Testwave> page){
	    LambdaQueryWrapper<Testwave> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Testwave::getId, request.getId());
	      }               
             
        if (StringUtils.hasText(request.getFnumber())) {
			    lambdaQueryWrapper.like(Testwave::getFnumber, request.getFnumber()); 
		    } 
             
        if (StringUtils.hasText(request.getFname())) {
			    lambdaQueryWrapper.like(Testwave::getFname, request.getFname()); 
		    } 
             
        if (StringUtils.hasText(request.getFcreateuser())) {
			    lambdaQueryWrapper.like(Testwave::getFcreateuser, request.getFcreateuser()); 
		    } 
             
        if (request.getFcreatedate() != null){
        	Date[] fcreatedate=request.getFcreatedate();
        	if(fcreatedate.length>0)
        	  lambdaQueryWrapper.ge(Testwave::getFcreatedate, fcreatedate[0]); 
        	if(fcreatedate.length==2)
        	  lambdaQueryWrapper.le(Testwave::getFcreatedate,fcreatedate[1]);      
        	
        }    
             
        if (StringUtils.hasText(request.getFaudituser())) {
			    lambdaQueryWrapper.like(Testwave::getFaudituser, request.getFaudituser()); 
		    } 
             
        if (request.getFauditdate() != null){
        	Date[] fauditdate=request.getFauditdate();
        	if(fauditdate.length>0)
        	  lambdaQueryWrapper.ge(Testwave::getFauditdate, fauditdate[0]); 
        	if(fauditdate.length==2)
        	  lambdaQueryWrapper.le(Testwave::getFauditdate,fauditdate[1]);      
        	
        }    
             
	    if (null != request.getFstatus()) {
	       lambdaQueryWrapper.eq(Testwave::getFstatus, request.getFstatus());
	      }               
             
	    if (null != request.getFtype()) {
	       lambdaQueryWrapper.eq(Testwave::getFtype, request.getFtype());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(Testwave::getId);	
		Page<Testwave> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	/**
	 * 新增测试波形
	 * @param userName
	 * @param header
	 * @param dataList
	 * @return
	 */
	@Override
	public boolean savetestwave(String userName, Map<String, Object> header, List<Map<String, Object>> dataList) {
		//获取当前日期
		Date currentDate = new Date();
		List<Testwave> testwaveList = new ArrayList<>();
		Testwave testwave = new Testwave();
		String fbillno = IXqcConst.createBillNo(BillType.CSBX);
		if (header!=null)
		{
//			testwave.setFnumber(VtuzxUtil.getString(header,"fnumber"));
			testwave.setFnumber(fbillno);
			testwave.setFstatus(0);//单据类型
			testwave.setFcreatedate(currentDate);
			testwave.setFcreateuser(userName);
			testwave.setFupdatedate(currentDate);
			testwave.setFupdateuser(userName);
			testwave.setFname(VtuzxUtil.getString(header,"fname"));
			testwave.setFnote(VtuzxUtil.getString(header,"fnote"));
//			String imagePath = VtuzxUtil.getString(header,"fpicture");  // 替换为你的图片路径
//			byte[] imageBytes = convertImageToBytes(imagePath);
			testwave.setFpictureId(VtuzxUtil.getInt(header,"fpicture"));
			testwave.setFtype(0);//启用状态未启用
			testwave.setFvs(VtuzxUtil.getString(header,"fvs"));
			testwave.setFvcom(VtuzxUtil.getString(header,"fvcom"));
			testwaveList.add(testwave);
		}
		boolean res = testwaveService.saveBatch(testwaveList);
		List<Testwavest> detailList =new ArrayList<>();
		long detailid = testwave.getId();
		List<Vswave> data = new ArrayList<>();
		for (Map<String, Object> map : dataList) {
			Testwavest detail = new Testwavest();
			detail.setDetailid(detailid);
			detail.setFdianya(VtuzxUtil.getString(map, "fdianya"));
			detail.setFboxing(VtuzxUtil.getString(map, "fboxing"));
			detail.setFngtype(VtuzxUtil.getInt(map, "fngtype"));
			detail.setFemperature(VtuzxUtil.getDouble(map, "femperature"));
			detail.setFresidtime(VtuzxUtil.getDouble(map, "fresidtime"));
			detail.setFrefreshtime(VtuzxUtil.getDouble(map, "frefreshtime"));
			detail.setFtuopanqty(VtuzxUtil.getLong(map, "ftuopanqty"));
			detail.setId(VtuzxUtil.getLong(map, "id"));
			detailList.add(detail);
		}
		boolean resdetail = testwavestService.saveOrUpdateBatch(detailList);
		return  res && resdetail;
	}

	/**
	 * 修改测试波形
	 * @param userName
	 * @param header
	 * @param dataList
	 * @return
	 */
	@Override
	public boolean updatetestwave(String userName, Map<String, Object> header, List<Map<String, Object>> dataList) {
		//获取当前日期
		Date currentDate = new Date();
		List<Testwave> testwaveList = new ArrayList<>();
		Testwave testwave = new Testwave();
		if (header!=null)
		{
			testwave.setId((VtuzxUtil.getLong(header,"id")));
			testwave.setFnumber(VtuzxUtil.getString(header,"fnumber"));
			testwave.setFstatus(0);
			testwave.setFupdatedate(currentDate);
			testwave.setFupdateuser(userName);
			testwave.setFname(VtuzxUtil.getString(header,"fname"));
			testwave.setFnote(VtuzxUtil.getString(header,"fnote"));
			testwave.setFpictureId(VtuzxUtil.getInt(header,"fpicture"));
			testwave.setFtype(0);
			testwave.setFvs(VtuzxUtil.getString(header,"fvs"));
			testwave.setFvcom(VtuzxUtil.getString(header,"fvcom"));
			testwaveList.add(testwave);
		}
		boolean res = testwaveService.saveOrUpdateBatch(testwaveList);
		List<Testwavest> detailList = new ArrayList<>();
		List<Testwave> data = new ArrayList<>();
		for (Map<String, Object> map : dataList) {
			Testwavest detail = new Testwavest();
			detail.setDetailid(VtuzxUtil.getLong(map, "detailid"));
			detail.setFdianya(VtuzxUtil.getString(map, "fdianya"));
			detail.setFboxing(VtuzxUtil.getString(map, "fboxing"));
			detail.setFngtype(VtuzxUtil.getInt(map, "fngtype"));
			detail.setFemperature(VtuzxUtil.getDouble(map, "femperature"));
			detail.setFresidtime(VtuzxUtil.getDouble(map, "fresidtime"));
			detail.setFrefreshtime(VtuzxUtil.getDouble(map, "frefreshtime"));
			detail.setFtuopanqty(VtuzxUtil.getLong(map, "ftuopanqty"));
			detail.setId(VtuzxUtil.getLong(map, "id"));
			detailList.add(detail);
		}
		boolean resdetail = testwavestService.saveOrUpdateBatch(detailList);
		return  res && resdetail;
	}

	/**
	 * 查询测试波形数据
	 * @param id
	 * @return
	 */
	@Override
	public VtuzxMap getDetail(Long id) {
		VtuzxMap data = new VtuzxMap();
		Testwave testwave =testwaveDao.selectById(id);
		//查询明细
		List<Testwavest> testwavestList =testwavestDao.selectList(new QueryWrapper<Testwavest>().eq("detailid",id));
		data.append("header",testwave)
				.append("dataList",testwavestList);
		return data;
	}



		public static byte[] convertImageToBytes(String imagePath) {
			byte[] imageBytes = null;

			try {
				Path path = Paths.get(imagePath);
				imageBytes = Files.readAllBytes(path);
			} catch (Exception e) {
				e.printStackTrace();
			}

			return imageBytes;
		}

}